package com.kefa.lock_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.kefa.lock.LockManager;
import com.kefa.lock_demo.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private boolean success = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());


    }

    public void openPort(View view) {
        if (success) return;
        /**
         * 如果ttyS3没用 就换 ttyS1,ttyS2,ttyS4
         */
        success = LockManager.getInstance().initialize("ttyS3");
        if (success) {
            Toast.makeText(this, "串口打开成功!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "串口打开失败!", Toast.LENGTH_SHORT).show();
        }
    }

    public void send(View view) {
        if (!success) {
            Toast.makeText(this, "请先打开串口!", Toast.LENGTH_SHORT).show();
        }
        String lockPlateNumberValues = binding.lockPlateNumber.getText().toString();
        String lockNumberValues = binding.lockNumber.getText().toString();
        int lockPlateNumber;
        int lockNumber;
        if (TextUtils.isEmpty(lockPlateNumberValues)) {
            Toast.makeText(this, "请输入锁板号!", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(lockNumberValues)) {
            Toast.makeText(this, "请输入锁号!", Toast.LENGTH_SHORT).show();
            return;
        }
        lockPlateNumber = Integer.parseInt(lockPlateNumberValues);
        lockNumber = Integer.parseInt(lockNumberValues);
        LockManager.getInstance().openLock(lockPlateNumber, lockNumber);
    }


}